/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package eshop

import (
    "github.com/mjiulee/lego"
    "github.com/go-xorm/xorm"
    //"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *ShopAccountLog ) RecodeById(id int64) *ShopAccountLog  {
	item := new(ShopAccountLog )
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *ShopAccountLog ) AddRecode(item2add *ShopAccountLog ) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopAccountLog ) AddRecodeWithsession(session*xorm.Session,item2add *ShopAccountLog ) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *ShopAccountLog ) DelRecodeById(id int64) bool {
	item := new(ShopAccountLog )
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopAccountLog ) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(ShopAccountLog )
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)*/
func (t *ShopAccountLog ) SDelRecodeById(id int64) bool {
	item := new(ShopAccountLog )
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopAccountLog ) SDelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(ShopAccountLog )
	_, err := session.ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}



/* 更新
 * -----------------*/
func (t *ShopAccountLog ) UpdateRecode(rc *ShopAccountLog ) bool {
	_, err := lego.GetDBEngine().ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopAccountLog) UpdateRecodeFields(rc *ShopAccountLog, cols ...string) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Cols(cols...).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopAccountLog ) UpdateRecodeWithSession(session *xorm.Session,rc *ShopAccountLog ) bool {
	_, err := session.ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/*查询表名, 注意，函数名不能定义未TableName()，因为会与xorm内部使用的TableName冲突*/
func (t *ShopAccountLog ) TName() string {
	tname := lego.GetDBEngine().TableName(t)
	return tname
}